package com.ssm.driver.controller;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.ssm.common.utils.JWTUtil;
import com.ssm.driver.entity.vo.OrderDto;
import com.ssm.driver.entity.vo.SoOrderDto;
import com.ssm.driver.entity.vo.SodetailDto;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.ssm.driver.entity.SsmSjEntity;
import com.ssm.driver.service.SsmSjService;
import com.ssm.common.utils.PageUtils;
import com.ssm.common.utils.R;
import javax.annotation.Resource;

import static com.ssm.common.utils.Constant.LOGIN_CODE;


/**
 * 
 *
 * @author ssm
 * @email sunlightcs@gmail.com
 * @date 2023-03-19 17:24:28
 */
@RestController
@RequestMapping("driver/ssmsj")
@Log4j2
public class SsmSjController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SsmSjService ssmSjService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = ssmSjService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 审核列表
    */
    @RequestMapping("/list2")
    public R findPage(@RequestParam Map<String, Object> params){
        PageUtils page = ssmSjService.findPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 模糊查询
     * */
    @RequestMapping("/findone")
    public R findone(@RequestParam Map<String, Object> params ){
        PageUtils page = ssmSjService.findone(params);
        return R.ok().put("page", page);
    }
    @RequestMapping("/findone1")
    public R findone1(@RequestParam Map<String, Object> params){
        PageUtils page = ssmSjService.findone1(params);
        return R.ok().put("page", page);
    }
    /**
     *冻结
     */

    @RequestMapping("/freeze")
    public R freeze(@RequestBody Integer[] sjIds){
        ssmSjService.freeze(sjIds);
        return R.ok();
    };

    /**
     * 解冻
     * @param sjIds
     * @return
     */
    @RequestMapping("/thaw")
    public R thaw(@RequestBody Integer[] sjIds){
        ssmSjService.thaw(sjIds);
        return R.ok();
    };
    /**
     * 信息
     */
    @RequestMapping("/info/{sjId}")
    public R info(@PathVariable("sjId") Integer sjId){
		SsmSjEntity ssmSj = ssmSjService.getById(sjId);

        return R.ok().put("ssmSj", ssmSj);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody Map ssmSj){
        Map<String,String> sjmap= (Map<String, String>) ssmSj.get("sjData");
        SsmSjEntity ssmSjEntity = new SsmSjEntity();
        ssmSjEntity.setSjName(sjmap.get("username").toString());
        ssmSjEntity.setSjPhone(sjmap.get("phone").toString());
        ssmSjEntity.setSjDrivie(sjmap.get("diver").toString());
        ssmSjEntity.setSjCardid(sjmap.get("cardId").toString());
        ssmSjEntity.setSjOperat(sjmap.get("opera").toString());
        ssmSjEntity.setInvited(RandomStringUtils.random(5,true,true));
        ssmSjEntity.setEdinvited(sjmap.get("invited"));
        System.out.println(ssmSjEntity.toString());
		ssmSjService.save(ssmSjEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody Map ssmSj){
        Map<String,String> sjmap= (Map<String, String>) ssmSj.get("sjData");
        SsmSjEntity ssmSjEntity = new SsmSjEntity();
        ssmSjEntity.setSjName(sjmap.get("username").toString());
        ssmSjEntity.setSjPhone(sjmap.get("phone").toString());
        ssmSjEntity.setSjDrivie(sjmap.get("diver").toString());
        ssmSjEntity.setSjCardid(sjmap.get("cardId").toString());
        ssmSjEntity.setSjOperat(sjmap.get("opera").toString());
        ssmSjEntity.setSjState("2");
		ssmSjService.updateById(ssmSjEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] sjIds){
		ssmSjService.removeByIds(Arrays.asList(sjIds));

        return R.ok();
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @PostMapping("sendcode")
    public R sendcode(@RequestBody Map phone){
        String numphone= (String) phone.get("phone");
        return ssmSjService.sendcode(numphone);
    }

    /**
     * 登录
     * @param loginForm
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody Map loginForm){
        String userphomne= (String) loginForm.get("phone");
        String usercode= (String) loginForm.get("code");
        String cahaCode= stringRedisTemplate.opsForValue().get(LOGIN_CODE+userphomne);
        if (!usercode.equals(cahaCode)) {
            return R.error("验证码错误");
        }
        SsmSjEntity ssmSjEntity=ssmSjService.finduser(userphomne);
        if (ssmSjEntity != null){
            String token = JWTUtil.generateToken(ssmSjEntity.getSjPhone());
            String userid= String.valueOf(ssmSjEntity.getSjId());
            //生成refreshToken
            //  refreshToken:{token:xxx,username:xxxx}
            String refreshToken = UUID.randomUUID().toString().replace("-", "");
            stringRedisTemplate.opsForHash().put(refreshToken, "token", token);
            stringRedisTemplate.opsForHash().put(refreshToken, "userphnoe", userphomne);
            stringRedisTemplate.opsForHash().put(refreshToken, "sjId",userid);
            //设置token的过期时间
            stringRedisTemplate.expire(refreshToken, JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);

            return R.ok().put("token",token).put("refreshToken",refreshToken);
        } else {
            return R.error("账号不存在请注册");
        }


    }

    /**
     * 司机个人数据查询
     * @param token
     * @return
     */
    @RequestMapping("/seachone")
    public R seachone(@RequestBody Map token){
        String refreshToken= (String) token.get("refreshToken");
        String sjId = (String) stringRedisTemplate.opsForHash().get(refreshToken, "sjId");
        SsmSjEntity ssmSj = ssmSjService.getById(sjId);
        return R.ok().put("ssmSj",ssmSj);
    }

    /**
     * 查询供货订单
     * @param token
     * @return
     */
    @RequestMapping("/getorder/{token}")
    public R getorder(@PathVariable("token") String token){
        String sjId = (String) stringRedisTemplate.opsForHash().get(token, "sjId");
        ArrayList<OrderDto> orderList=ssmSjService.getOrder(sjId);
    return R.ok().put("orderList",orderList);
    }

    /**
     * 查询销售订单
     * @param token
     * @return
     */
    @RequestMapping("/getsoorder/{token}")
    public R getsoorder(@PathVariable("token") String token){
        String sjId = (String) stringRedisTemplate.opsForHash().get(token, "sjId");
        ArrayList<SoOrderDto> orderList = ssmSjService.getsoOrder(sjId);
        return R.ok().put("orderList",orderList);
    }
    @RequestMapping("/getdetail/{id}")
    public R getdetail(@PathVariable("id") String id) {
        ArrayList<SodetailDto>detailList = ssmSjService.getDetails(id);
        return R.ok().put("detailList",detailList);
    }


}
